package christ.mmd.xml;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import christ.mmd.bl.Fast;
import christ.mmd.bl.FileUpdateInfo;

import java.text.ParseException;
import android.text.format.DateFormat;
import android.util.Xml;

public class FastXmlParser extends AXmlParser {

	protected List readDataEntities(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		List entries = new ArrayList();

		parser.require(XmlPullParser.START_TAG, ns, "fasts");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			// Starts by looking for the entry tag
			if (name.equals("fast")) {
				entries.add(readFastEntry(parser));
			} else {
				skip(parser);
			}
		}
		return entries;
	}

	// Processes file tags in the feed.
	private Fast readFastEntry(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "fast");

		String id = parser.getAttributeValue(null, "id");
		String relType = parser.getAttributeValue(null, "type");
		String dateBeginStr = parser.getAttributeValue(null, "dateBegin");
		String dateEndStr = parser.getAttributeValue(null, "dateEnd");
		String description = "";
		parser.nextTag();
		String tagName = parser.getName();
		// Starts by looking for the entry tag
		if (tagName.equals("description")) {
			parser.require(XmlPullParser.START_TAG, ns, "description");
			description = readText(parser);
			parser.require(XmlPullParser.END_TAG, ns, "description");
		}
		parser.nextTag();
		parser.require(XmlPullParser.END_TAG, ns, "fast");
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
		Date dateBegin = null;
		try {
			dateBegin = format.parse(dateBeginStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date dateEnd = null;
		try {
			dateEnd = format.parse(dateEndStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long idLong = id.isEmpty() ? -1 : Long.parseLong(id);
		Fast result = new Fast(idLong, relType, dateBegin, dateEnd, description);
		return result;
	}
}
